home *** CD-ROM | disk | FTP | other *** search
- /* sortio.c - I/O functions for sorttext program */
- /* do_open , do_close , getrec , putrec */
- #include "stdio.h"
- #include "cminor.h"
-
- FILE *gfopen() ;
-
- FILE *do_open(fname,fmode) /* open a file and check for errors */
- char fname[] ; /* file name */
- char fmode[] ; /* read/write/append mode */
- { /* return a file pointer */
- FILE *fd ;
-
- fd = gfopen(fname,fmode,ASC_MODE) ;
- if( fd == NULL )
- { printf("\n can't open file - %s \n",fname) ;
- exit( 8 ) ;
- }
- return( fd ) ;
- }
-
- int do_close(fd,fname) /* close file and check for errors */
- FILE *fd ; /* file pointer */
- char fname ; /* name of file being closed */
- {
- if( fclose(fd) < 0 )
- { printf("\n can't close file - %s \n",fname) ;
- exit( 10 ) ;
- }
- }
-
-
- int getrec(rec,maxr,fd)
- char rec[] ; /* put it here in string form */
- int maxr ; /* maximum length permitted */
- FILE *fd ; /* file pointer to input file */
- { /* return no. of chars used in rec */
- return( getl(rec,maxr,fd));/* let getl do the work */
- }
-
-
- int putrec(rec,fd) /* output one record */
- char rec[] ; /* the record to output */
- FILE *fd ; /* output file pointer */
- {
- return( putl(rec,fd) ) ; /* use putl to do line output */
- }
-
-
- /* ******************************* line oriented functions */
-
- int getl(s,maxs,fd) /* get one line from input file */
- char s[] ; /* put it here in string form */
- int maxs ; /* maximum length permitted */
- FILE *fd ; /* file pointer for input file */
- { /* getl returns no. chars used in s */
- /* ( or -1 if EOF reached */
- int i ;
- /* get next line of input */
- if( fgets(s,maxs-1,fd) == NULL )
- return( -1 ) ; /* EOF - return special length value */
-
- i = strlen(s) ; /* get string length */
- if( s[i-1] != '\n' ) /* see if a new-line is present */
- { s[i] = '\n' ; /* n - append one */
- s[i+1] = '\0' ; /* restore end of string marker */
- i = i + 1 ; /* adjust string length */
- }
- /* return length of line */
- return(i+1) ; /* count the '\o' at end too */
- }
-
-
- int putl(s,fd) /* output one line of text */
- char s[] ; /* line to output in string form */
- FILE *fd ; /* output file pointer */
- {
- return( fputs(s,fd) ) ; /* use fputs libary function */
- }
-
-
-